#include <iostream>
#include <string>
using namespace std;

#include "Vocabulary.hh"

Vocabulary::Vocabulary(){
  id_so_far = 0;

  // add NULL token
  token_to_id["NULL"] = 0;
  id_to_token[0] = "NULL";
}

void Vocabulary::add_to_vocabulary(vector<string> &tokens){
  for(vector<string>::iterator i = tokens.begin(); i != tokens.end(); i++){
    string &token = *i;
    if(token_to_id.find(token) == token_to_id.end()){
      // new vocabulary!
      token_to_id[token] = ++id_so_far;
      id_to_token[id_so_far] = token;
    }
  }
}

string Vocabulary::map_to_token(int id){
  if(id_to_token.find(id) == id_to_token.end()){
    cerr << "id not found in vocabulary!" << endl;
    exit(1);
  }
  return id_to_token[id];
}

int Vocabulary::map_to_id(string &token){
  if(token_to_id.find(token) == token_to_id.end()){
    cerr << "token not found in vocabulary!" << endl;
    exit(1);
  }
  return token_to_id[token];
}

void Vocabulary::map_to_ids(vector<string> &tokens, vector<int> &ids){
  ids.clear();

  for(vector<string>::iterator i = tokens.begin(); i != tokens.end(); i++){
    ids.push_back(map_to_id(*i));
  }
}

void Vocabulary::map_to_tokens(vector<int> &ids, vector<string> &tokens){
  tokens.clear();

  for(vector<int>::iterator i = ids.begin(); i != ids.end(); i++){
    tokens.push_back(map_to_token(*i));
  }
}
